package com.htfyun.factorytest.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.htfyun.factorytest.R;
import com.htfyun.factorytest.utils.DebugLog;

public class CrossView extends View {
    private int m_iLength = 25;
    private int m_iWidth = 10;
    private int m_iColor = Color.BLACK;
    private int m_iRadius = 10;
    private Context m_context;
    private Paint m_paint;
    private int m_pointX = 100;
    private int m_pointY = 100;
    private int deviation = 9;

    public CrossView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public CrossView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CrossView(Context context, int x, int y) {
        super(context);
        m_pointX = x;
        m_pointY = y;
        init(context, null);
    }

    public CrossView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.m_context = context;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        deviation = (int) (metrics.densityDpi / 25.4) + 1;
        DebugLog.e("deviation = " + deviation);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CrossView);
        m_iLength = (int) a.getDimension(R.styleable.CrossView_lineLength, 50);
        m_iWidth = (int) a.getDimension(R.styleable.CrossView_lineWidth, 3);
        m_iColor = a.getColor(R.styleable.CrossView_lineColor, Color.BLACK);

        m_paint = new Paint();
        m_paint.setColor(m_iColor);
        m_paint.setStrokeWidth(m_iWidth);
        m_paint.setAntiAlias(true);
        if (m_iWidth > m_iRadius * 2)
            m_iRadius = m_iWidth / 2;
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // method1(canvas);
        method2(canvas);
    }

    public boolean testInRange(int x, int y) {
        int deviationX = Math.abs(m_pointX - x);
        int deviationY = Math.abs(m_pointY - y);
//        DebugLog.e("deviationX = " + deviationX + " deviationY = " + deviationY);
        return deviationX < deviation && deviationY < deviation;
    }

    private void method2(Canvas canvas) {
        // 2.画横线
        // 2.1左边
        int startX = m_pointX - (m_iLength + m_iRadius * 2) / 2;
        int startY = m_pointY;
        int stopX = startX + m_iLength / 2;
        int stopY = startY;
        canvas.drawLine(startX, startY, stopX, stopY, m_paint);
        // 2.2右边
        startX = stopX + m_iRadius * 2;
        stopX = startX + m_iLength / 2;
        canvas.drawLine(startX, startY, stopX, stopY, m_paint);

        // 3.画竖线
        // 3.1 左边
        startX = m_pointX;
        startY = m_pointY - (m_iLength + m_iRadius * 2) / 2;
        stopX = startX;
        stopY = startY + m_iLength / 2;
        canvas.drawLine(startX, startY, stopX, stopY, m_paint);
        // 3.2右边
        startY = stopY + m_iRadius * 2;
        stopY = startY + m_iLength / 2;
        canvas.drawLine(startX, startY, stopX, stopY, m_paint);

        // 4.画圆
        int radius = 5;
        canvas.drawCircle(m_pointX, m_pointY, radius, m_paint);
    }
}
